package com.dong.module.menulog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dong.common.util.FileUtils;
import com.dong.module.menulog.dao.MenuLogMapper;
import com.dong.module.menulog.pojo.MenuLog;
import com.dong.module.menulog.service.MenuLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @author: 陈瑞东
 * @create: 2021-08-25 08:36
 */
@Service
public class MenuLogServiceImpl extends ServiceImpl<MenuLogMapper, MenuLog> implements MenuLogService {


    @Autowired
    private MenuLogMapper menuLogMapper;

    @Override
    public int countLog(String foodId) {
        return (int) count(new QueryWrapper<MenuLog>().eq("food_id", foodId));
    }

    @Override
    public void save(String id, String foodId, String description, String filename) {
        if (StringUtils.isBlank(id)) {
            // 添加
            int count = countLog(foodId) + 1;
            String image = FileUtils.saveImage(foodId, filename);
            MenuLog menuLog = new MenuLog(foodId, image, description, count);
            save(menuLog);
        } else {
            // 修改
            MenuLog menuLog = getById(id);
            menuLog.setDescription(description);
            if (!filename.equals(menuLog.getImage())) {
                String image = FileUtils.saveImage(foodId, filename);
                menuLog.setImage(image);
            }
            updateById(menuLog);
        }
    }
}
